package com.jason.frescotest;

import android.annotation.TargetApi;
import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.jason.frescotest.utils.ImageURL;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {


    private SimpleDraweeView simpleDraweeView;
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        simpleDraweeView = (SimpleDraweeView) findViewById(R.id.simple_draweeview);
        findViewById(R.id.next).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ListDemoActivity.class);
                startActivity(intent);

            }
        });

        findViewById(R.id.grid).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, GridActivity.class);
                startActivity(intent);
            }
        });


        //GenericDraweeHierarchy hierarchy = simpleDraweeView.getHierarchy();
        //hierarchy.setFailureImage(getResources().getDrawable(R.mipmap.ic_launcher, null)); //设置加载失败的图片

        /*

        设置圆角图标
        GenericDraweeHierarchyBuilder builder = new GenericDraweeHierarchyBuilder(getResources());
        RoundingParams roundingParams = RoundingParams.fromCornersRadius(20);
        roundingParams.setBorder(Color.TRANSPARENT, 1.0f);
        builder.setRoundingParams(roundingParams);
        GenericDraweeHierarchy build = builder.build();
        simpleDraweeView.setHierarchy(build);

        对应的xml文件可以设置
        fresco:roundedCornerRadius="20dp"
        fresco:roundingBorderWidth="1dp"

        */

        //Uri uri = Uri.parse(ImageURL.imageUrl[2]);
        //simpleDraweeView.setImageURI(uri);

        initGif();





    }


    private void initGif() {
        Uri uri = Uri.parse(ImageURL.imageUrl[2]);
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri).build();
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setImageRequest(request)
                .setAutoPlayAnimations(true)
                .build();

        simpleDraweeView.setController(controller);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);


    }
}
